package com.hyq.controller;

import com.hyq.model.SUser;

import com.hyq.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    public StudentService service;


    //验证登录时用户名是否重复
    @RequestMapping("/validateName")
    @ResponseBody
    public boolean validateName(String name){
        return service.validateName(name);

    }

    //学生注册
    @RequestMapping("/register")
    public String sRegister(SUser su){

        int val = service.sRegister(su);
        if(val > 0){
            return "sLogin";
        }else{
            return "sRegister";
        }

    }

    //学生登录
    @RequestMapping("/login")
    public String sLogin(SUser su, HttpServletRequest request){
        SUser newSu = service.sLogin(su);
        if(newSu != null){
            request.getSession().setAttribute("SUserInfo", newSu);
            return "sMain";
        }else{
            return "sLogin";
        }


    }

    //学生注销
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request){
        request.getSession().removeAttribute("SUserInfo");
        return "sMain";
    }


    //学生更改密码
    @RequestMapping("/updatePassword")
    @ResponseBody
    public String updatePassword(String oldPassword,String newPassword,int sid){

        int val1 = service.cOldPassword(oldPassword, sid);

        if(val1 > 0){
            int val2 = service.updatePassword(newPassword, sid);
            if(val2 > 0){
                return "Modified success!";
            }else{
                return "Modification failed!";
            }
        }else{
            return "Original password error!";
        }
    }

//想做学生修改头像，但是没成功
//	@RequestMapping("supdate")
//	public String supdate(@RequestParam(value="img",required=false)CommonsMultipartFile myfile,SUser su,HttpServletRequest request ){
//
//		System.out.println(su);
//		try {
//			String oldName = myfile.getOriginalFilename();
//			String suffix = oldName.substring(oldName.lastIndexOf("."));
//			long time = System.currentTimeMillis();
//			String newName = time + "" + Math.floor(Math.random()*10000);
//			myfile.getFileItem().write(new File("D:/pic/" + newName + suffix));
//
//			su.setSimg("/kkk/" + newName + suffix);
//
//			int val = service.supdate(su);
//			SUser newSu = service.ssearchById(su.getSid());
//			request.getSession().setAttribute("SUserInfo", newSu);
//
//			return "sPersonal";
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		return "sPersonal";
//
//	}


    //学生修改用户名（目前这个功能不太行）
    @RequestMapping("/updateName")
    @ResponseBody
    public String updateName(String sname,int sid){
        int val1 = service.updateSname(sname, sid);
        if(val1 > 0){
            return "Modified success!";
        }else{
            return "Original password error!";
        }
    }



}
